﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar
{
    public class ucMonthContentItem : Control
    {
        #region Dependency Variables
        private static readonly DependencyProperty DisplayTextProperty = DependencyProperty.Register("DisplayText", typeof(string), typeof(ucMonthContentItem), new PropertyMetadata("", new PropertyChangedCallback(DisplayText_Changed)));
        private static readonly DependencyProperty MonthContentHeaderForegroundProperty = DependencyProperty.Register("MonthContentHeaderForeground", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(new SolidColorBrush(Colors.White), new PropertyChangedCallback(MonthContentHeaderForeground_Changed)));
        private static readonly DependencyProperty MonthContentHeaderBackgroundProperty = DependencyProperty.Register("MonthContentHeaderBackground", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(new SolidColorBrush(Colors.LightGray), new PropertyChangedCallback(MonthContentHeaderBackground_Changed)));
        private static readonly DependencyProperty MonthContentHeaderBorderBrushProperty = DependencyProperty.Register("MonthContentHeaderBorderBrush", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(new SolidColorBrush(Colors.DarkGray), new PropertyChangedCallback(MonthContentHeaderBorderBrush_Changed)));
        private static readonly DependencyProperty MonthContentHeaderBorderThicknessProperty = DependencyProperty.Register("MonthContentHeaderBorderThickness", typeof(Thickness), typeof(ucMonthContentItem), new PropertyMetadata(new Thickness(1, 1, 1, 2), new PropertyChangedCallback(MonthContentHeaderBorderThickness_Changed)));
        private static readonly DependencyProperty MonthContentBackgroundProperty = DependencyProperty.Register("MonthContentBackground", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#E6EDF7"), new PropertyChangedCallback(MonthContentBackground_Changed)));
        private static readonly DependencyProperty MonthContentBorderBrushProperty = DependencyProperty.Register("MonthContentBorderBrush", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#B0B6BE"), new PropertyChangedCallback(MonthContentBorderBrush_Changed)));
        private static readonly DependencyProperty MonthContentBorderThicknessProperty = DependencyProperty.Register("MonthContentBorderThickness", typeof(Thickness), typeof(ucMonthContentItem), new PropertyMetadata(new Thickness(1), new PropertyChangedCallback(MonthContentBorderThickness_Changed)));
        private static readonly DependencyProperty DayAppointmentForegroundProperty = DependencyProperty.Register("DayAppointmentForeground", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(new SolidColorBrush(Colors.White), new PropertyChangedCallback(DayAppointmentForeground_Changed)));
        private static readonly DependencyProperty DayAppointmentBackgroundProperty = DependencyProperty.Register("DayAppointmentBackground", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#10000000"), new PropertyChangedCallback(DayAppointmentBackground_Changed)));
        private static readonly DependencyProperty DayAppointmentBorderBrushProperty = DependencyProperty.Register("DayAppointmentBorderBrush", typeof(Brush), typeof(ucMonthContentItem), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#5D8CC9"), new PropertyChangedCallback(DayAppointmentBorderBrush_Changed)));
        private static readonly DependencyProperty DayAppointmentBorderThicknessProperty = DependencyProperty.Register("DayAppointmentBorderThickness", typeof(Thickness), typeof(ucMonthContentItem), new PropertyMetadata(new Thickness(1), new PropertyChangedCallback(DayAppointmentBorderThickness_Changed)));
        private static readonly DependencyProperty DayAppointmentListProperty = DependencyProperty.Register("DayAppointmentList", typeof(List<UIElement>), typeof(ucMonthContentItem), new PropertyMetadata(null, new PropertyChangedCallback(DayAppointmentList_Changed)));
        #endregion

        #region Dependency Properties
        public string DisplayText
        {
            get { return this.GetValue(DisplayTextProperty).ToString(); }
            set { this.SetValue(DisplayTextProperty, value); }
        }
        public Brush MonthContentHeaderForeground
        {
            get { return (Brush)this.GetValue(MonthContentHeaderForegroundProperty); }
            set { this.SetValue(MonthContentHeaderForegroundProperty, value); }
        }
        public Brush MonthContentHeaderBackground
        {
            get { return (Brush)this.GetValue(MonthContentHeaderBackgroundProperty); }
            set { this.SetValue(MonthContentHeaderBackgroundProperty, value); }
        }
        public Brush MonthContentHeaderBorderBrush
        {
            get { return (Brush)this.GetValue(MonthContentHeaderBorderBrushProperty); }
            set { this.SetValue(MonthContentHeaderBorderBrushProperty, value); }
        }
        public Thickness MonthContentHeaderBorderThickness
        {
            get { return (Thickness)this.GetValue(MonthContentHeaderBorderThicknessProperty); }
            set { this.SetValue(MonthContentHeaderBorderThicknessProperty, value); }
        }
        public Brush MonthContentBackground
        {
            get { return (Brush)this.GetValue(MonthContentBackgroundProperty); }
            set { this.SetValue(MonthContentBackgroundProperty, value); }
        }
        public Brush MonthContentBorderBrush
        {
            get { return (Brush)this.GetValue(MonthContentBorderBrushProperty); }
            set { this.SetValue(MonthContentBorderBrushProperty, value); }
        }
        public Thickness MonthContentBorderThickness
        {
            get { return (Thickness)this.GetValue(MonthContentBorderThicknessProperty); }
            set { this.SetValue(MonthContentBorderThicknessProperty, value); }
        }
        public Brush DayAppointmentForeground
        {
            get { return (Brush)this.GetValue(DayAppointmentForegroundProperty); }
            set { this.SetValue(DayAppointmentForegroundProperty, value); }
        }
        public Brush DayAppointmentBackground
        {
            get { return (Brush)this.GetValue(DayAppointmentBackgroundProperty); }
            set { this.SetValue(DayAppointmentBackgroundProperty, value); }
        }
        public Brush DayAppointmentBorderBrush
        {
            get { return (Brush)this.GetValue(DayAppointmentBorderBrushProperty); }
            set { this.SetValue(DayAppointmentBorderBrushProperty, value); }
        }
        public Thickness DayAppointmentBorderThickness
        {
            get { return (Thickness)this.GetValue(DayAppointmentBorderThicknessProperty); }
            set { this.SetValue(DayAppointmentBorderThicknessProperty, value); }
        }
        public List<UIElement> DayAppointmentList
        {
            get { return (List<UIElement>)this.GetValue(DayAppointmentListProperty); }
            set { this.SetValue(DayAppointmentListProperty, value); }
        }
        #endregion

        #region Dependency Events
        #region DisplayText_Changed
        private static void DisplayText_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDisplayText_Changed(e);
        }
        #endregion

        #region MonthContentHeaderForeground_Changed
        private static void MonthContentHeaderForeground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentHeaderForeground_Changed(e);
        }
        #endregion

        #region MonthContentHeaderBackground_Changed
        private static void MonthContentHeaderBackground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentHeaderBackground_Changed(e);
        }
        #endregion

        #region MonthContentHeaderBorderBrush_Changed
        private static void MonthContentHeaderBorderBrush_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentHeaderBorderBrush_Changed(e);
        }
        #endregion

        #region MonthContentHeaderBorderThickness_Changed
        private static void MonthContentHeaderBorderThickness_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentHeaderBorderThickness_Changed(e);
        }
        #endregion

        #region MonthContentBackground_Changed
        private static void MonthContentBackground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentBackground_Changed(e);
        }
        #endregion

        #region MonthContentBorderBrush_Changed
        private static void MonthContentBorderBrush_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentBorderBrush_Changed(e);
        }
        #endregion

        #region MonthContentBorderThickness_Changed
        private static void MonthContentBorderThickness_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnMonthContentBorderThickness_Changed(e);
        }
        #endregion

        #region DayAppointmentForeground_Changed
        private static void DayAppointmentForeground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDayAppointmentForeground_Changed(e);
        }
        #endregion

        #region DayAppointmentBackground_Changed
        private static void DayAppointmentBackground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDayAppointmentBackground_Changed(e);
        }
        #endregion

        #region DayAppointmentBorderBrush_Changed
        private static void DayAppointmentBorderBrush_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDayAppointmentBorderBrush_Changed(e);
        }
        #endregion

        #region DayAppointmentBorderThickness_Changed
        private static void DayAppointmentBorderThickness_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDayAppointmentBorderThickness_Changed(e);
        }
        #endregion

        #region DayAppointmentList_Changed
        private static void DayAppointmentList_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucMonthContentItem).OnDayAppointmentList_Changed(e);
        }
        #endregion
        #endregion

        #region Variables
        StackPanel spAppointments = null;
        Border brdHeader = null;
        #endregion

        #region Properties
        public DateTime CurrentDate { get; set; }
        #endregion

        #region Event Variables
        public event EventHandler DayHeader_Clicked;
        #endregion

        #region Constructor
        public ucMonthContentItem()
        {
            DefaultStyleKey = typeof(ucMonthContentItem);
            DayAppointmentList = new List<UIElement>();
        }
        #endregion

        #region Methods
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            spAppointments = (StackPanel)base.GetTemplateChild("spAppointments");
            brdHeader = (Border)base.GetTemplateChild("brdHeader");

            if (spAppointments == null) return;            

            PopulateAppointments();

            if (brdHeader == null) return;

            brdHeader.MouseLeftButtonUp += new MouseButtonEventHandler(brdHeader_MouseLeftButtonUp);
        }
        #endregion

        #region PopulateAppointments
        private void PopulateAppointments()
        {
            if (spAppointments != null && DayAppointmentList != null && DayAppointmentList.Count > 0)
            {
                foreach(UIElement ele in DayAppointmentList)
                    spAppointments.Children.Add(ele);
            }
        }
        #endregion

        #region OnDisplayText_Changed
        protected virtual void OnDisplayText_Changed(DependencyPropertyChangedEventArgs e)
        {
            DisplayText = e.NewValue.ToString();
        }
        #endregion

        #region OnMonthContentHeaderForeground_Changed
        protected virtual void OnMonthContentHeaderForeground_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentHeaderForeground = (Brush)e.NewValue;
        }
        #endregion

        #region OnMonthContentHeaderBackground_Changed
        protected virtual void OnMonthContentHeaderBackground_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentHeaderBackground = (Brush)e.NewValue;
        }
        #endregion

        #region OnMonthContentHeaderBorderBrush_Changed
        protected virtual void OnMonthContentHeaderBorderBrush_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentHeaderBorderBrush = (Brush)e.NewValue;
        }
        #endregion

        #region OnMonthContentHeaderBorderThickness_Changed
        protected virtual void OnMonthContentHeaderBorderThickness_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentHeaderBorderThickness = (Thickness)e.NewValue;
        }
        #endregion

        #region OnMonthContentBackground_Changed
        protected virtual void OnMonthContentBackground_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentBackground = (Brush)e.NewValue;
        }
        #endregion

        #region OnMonthContentBorderBrush_Changed
        protected virtual void OnMonthContentBorderBrush_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentBorderBrush = (Brush)e.NewValue;
        }
        #endregion

        #region OnMonthContentBorderThickness_Changed
        protected virtual void OnMonthContentBorderThickness_Changed(DependencyPropertyChangedEventArgs e)
        {
            MonthContentBorderThickness = (Thickness)e.NewValue;
        }
        #endregion

        #region OnDayAppointmentForeground_Changed
        protected virtual void OnDayAppointmentForeground_Changed(DependencyPropertyChangedEventArgs e)
        {
            DayAppointmentForeground = (Brush)e.NewValue;
        }
        #endregion

        #region OnDayAppointmentBackground_Changed
        protected virtual void OnDayAppointmentBackground_Changed(DependencyPropertyChangedEventArgs e)
        {
            DayAppointmentBackground = (Brush)e.NewValue;
        }
        #endregion

        #region OnDayAppointmentBorderBrush_Changed
        protected virtual void OnDayAppointmentBorderBrush_Changed(DependencyPropertyChangedEventArgs e)
        {
            DayAppointmentBorderBrush = (Brush)e.NewValue;
        }
        #endregion

        #region OnDayAppointmentBorderThickness_Changed
        protected virtual void OnDayAppointmentBorderThickness_Changed(DependencyPropertyChangedEventArgs e)
        {
            DayAppointmentBorderThickness = (Thickness)e.NewValue;
        }
        #endregion

        #region OnDayAppointmentList_Changed
        protected virtual void OnDayAppointmentList_Changed(DependencyPropertyChangedEventArgs e)
        {
            DayAppointmentList = (List<UIElement>)e.NewValue;
            PopulateAppointments();
        }
        #endregion
        #endregion        

        #region Events
        #region brdHeader_MouseLeftButtonUp
        private void brdHeader_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DayHeader_Clicked != null)
                DayHeader_Clicked(this, null);
        }
        #endregion
        #endregion
    }
}
